// pages/audioRoom/audioRoom.js
import {audioRoomAPI} from '../../api/audioRoom/index'
import {$await} from '../../utils/util'
const CONFIG = require('../../config');
const {imgUrlAvatar} = require('../../config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //标题
	img_url:'https://images.weserv.nl/?url=',
	avatar:CONFIG.imgUrlAvatar,
	noType:'../../images/audioRoom/null.png',
	add:'../../images/audioRoom/add-room.png',
	gray:'../../images/audioRoom/gray-room.png',
	mine:'',
	roomList:[
		// {
		// 	title:'用打的方适教育孩子合适吗',
		// 	start:'2021-04-01 15:34:56',
		// 	num:'88',
		// 	name:'谢华',
		// 	btn:'管理房间',
		// 	master:1,
		// }
	],
	page:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  toRoom(e){
  	  let idx = e.target.dataset.index;
  	  let roomList = this.data.roomList;
  	  let roomNumber = roomList[idx].room_number;
  	  if(roomList[idx].status==0){
  		this.orderRoom(roomNumber,0)//取消预约
  	  }else{
  		wx.navigateTo({
  			url:'/pages/audioRoom/audioRoom?roomNumber='+roomNumber+'&from1=wxapp'
  		})   
  	  }
  	  
  },
  // 获取房间列表
  async roomList(page,pageSize){
  	   let data = {page,pageSize}
  	   let [err,res] = await $await(audioRoomAPI.resList(data));
  	   if(res.length>0){
  	   	this.setData({
  	   		roomList: res,
  	   	})	
  	   }else{
			let page = this.data.page-1
			if(page<1) page=1
			this.setData({
				roomList: res,
				page
			}) 
	   }	   
  },
  // 预约房间
  async orderRoom(room_number,reserve_type){
  	   let data = {room_number,reserve_type}
  	   let [err,res] = await $await(audioRoomAPI.orderRoom(data));
  	   if(res.status==1 && reserve_type==1){
  			wx.showToast({
  				 title: '预约成功',
  				 icon: "none",
  				 duration: 1000
  			})
  	   }else if(res.status==1 && reserve_type==0){
  		   wx.showToast({
  		   	 title: '取消成功',
  		   	 icon: "none",
  		   	 duration: 1000
  		   })
  	   }
  	   this.roomList(this.data.page,10)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	this.roomList(this.data.page,10)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  	wx.showToast({
  	      title: 'loading...',
  	      icon: 'loading'
  	})
  	this.roomList(this.data.page,10)
  	setTimeout(function(){
  		wx.stopPullDownRefresh({
  		  complete(res) {
  		    wx.hideToast()
  		    console.log(res, new Date())
  		  }
  		})
  	},500)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
	let page = this.data.page += 1;
	this.roomList(page,10)
	console.log(page)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})